#include <ilcplex/ilocplex.h>
ILOSTLBEGIN

static void
   usage (const char *progname),
   populatebynonzero (IloModel model, IloIntVarArray var, IloRangeArray con);

int
main (int argc, char **argv)
{
   IloEnv   env;
   try {
      IloModel model(env);

      if (( argc != 2 )                         ||
          ( argv[1][0] != '-' )                 ||
          ( strchr ("rcn", argv[1][1]) == NULL )   ) {
         usage (argv[0]);
         throw(-1);
      }

      IloIntVarArray var(env);
      IloRangeArray con(env);

      switch (argv[1][1]) {
         case 'n':
            populatebynonzero (model, var, con);
            break;
      }

      IloCplex cplex(model);

      // Optimize the problem and obtain solution.
      if ( !cplex.solve() ) {
         env.error() << "Failed to optimize LP" << endl;
         throw(-1);
      }

      IloNumArray vals(env);
      env.out() << "Solution status = " << cplex.getStatus() << endl;
      env.out() << "Solution value  = " << cplex.getObjValue() << endl;
      cplex.getValues(vals, var);
      env.out() << "Values        = " << vals << endl;
      cplex.getSlacks(vals, con);
      env.out() << "Slacks        = " << vals << endl;
      //cplex.getDuals(vals, con);
      env.out() << "Duals         = " << vals << endl;
      //cplex.getReducedCosts(vals, var);
      env.out() << "Reduced Costs = " << vals << endl;

      cplex.exportModel("lpex1.lp");
   }
   catch (IloException& e) {
      cerr << "Concert exception caught: " << e << endl;
   }
   catch (...) {
      cerr << "Unknown exception caught" << endl;
   }

   env.end();

   return 0;
}  // END main


static void usage (const char *progname)
{
   cerr << "Usage: " << progname << " -X" << endl;
   cerr << "   where X is one of the following options:" << endl;
   cerr << "      n          generate problem by nonzero" << endl;
   cerr << " Exiting..." << endl;
} // END usage


static void
populatebynonzero (IloModel model, IloIntVarArray x, IloRangeArray c)
{
   IloEnv env = model.getEnv();

   IloObjective obj = IloMaximize(env);
	int n, a;
	scanf("%d", &n);

		
	scanf("%d", &a);
	//restrição
   c.add(IloRange(env, -IloInfinity, a));

	//variaveis
	for(int i=0 ; i<n; i++){
		x.add(IloIntVar(env, 0, 1));
	}

	/*restricoes*/
	for(int i=0 ; i<n; i++){
		scanf("%d", &a);
		c[0].setLinearCoef(x[i], a);
	}

	//objetivo	
	for(int i=0 ; i<n; i++){
		scanf("%d", &a);
		obj.setLinearCoef(x[i], a);
	}
   
   c[0].setName("c1");
	for(int i=0; i<n; i++){
		char tmp[10];
		sprintf( tmp, "x%d", i+1);
		x[i].setName(tmp);	
	}

   model.add(obj);
   model.add(c);
}  // END populatebynonzero
